package com.chushouya.product.dao.repository;

import com.general.framework.core.lang.Lists;
import com.general.framework.core.lang.Strings;
import com.general.framework.core.schema.PageResult;
import com.general.framework.data.mybatis.BaseRepository;
import com.chushouya.product.dao.entity.BrandEntity;
import com.chushouya.product.dao.mapper.BrandEntityMapper;
import com.chushouya.product.dto.admin.brand.BrandQuery;
import org.springframework.stereotype.Repository;
import tk.mybatis.mapper.entity.Example;

import java.util.List;
import java.util.Objects;


@Repository
public class BrandRepository extends BaseRepository<BrandEntityMapper, BrandEntity, Long> {
    public List<BrandEntity> selectList(BrandQuery query) {
        Example example = buildExample(query);
        return this.selectByExample(example);
    }

    public PageResult<BrandEntity> selectPageResult(BrandQuery query) {
        Example example = this.buildExample(query);
        return this.selectPage(query.getPageRequest(), example);
    }
    public BrandEntity selectOne(BrandQuery query) {
        Example example = buildExample(query);
        return this.selectFirstOneByExample(example);
    }

    private Example buildExample(BrandQuery query) {
        Example example = new Example(BrandEntity.class);
        Example.Criteria criteria = example.createCriteria();
        if (Objects.nonNull(query.getBrandId())) {
            criteria.andEqualTo("brandId", query.getBrandId());
        }
        if(Lists.isNotEmpty(query.getBrandIdList())){
            criteria.andIn("brandId", query.getBrandIdList());
        }
        if (Strings.isNotEmpty(query.getBrandName())) {
            criteria.andEqualTo("brandName", query.getBrandName());
        }
        return example;
    }
//    public List<BrandEntity> selectListByCategoryIds(List<Long> categoryIds) {
//        return mapper.selectListByCategoryIds(categoryIds);
//    }
//
//    public List<BrandEntity> selectListByCategoryId(BrandHotApiQuery query) {
//        return mapper.selectListByCategoryId(query);
//    }

    public BrandEntity selectOneWithProperties(Long brandId, String... properties) {
        BrandQuery query = new BrandQuery();
        query.setBrandId(brandId);
        final Example example = this.buildExample(query);
        example.selectProperties(properties);
        return this.selectFirstOneByExample(example);
    }

    public BrandEntity selectByBrandName(BrandQuery query) {
        Example example = this.buildExample(query);
        return this.selectFirstOneByExample(example);
    }
}